package com.encol.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.encol.common.utils.R;
import com.encol.entity.User;
import com.encol.service.UserSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @Author:Encol
 * @Date:11:44 2021/9/24
 * @Description:
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserSerivce userSerivce;
//因为现在前后端分离，传递数据用JSON格式，所以看需要的参数前要加@RequestBody
    @PostMapping("/addUser")
    public R addUser(@RequestBody User user) {
        boolean b = userSerivce.save(user);
        return R.out(b);
    }

    @GetMapping("/deleteUser/{id}")
    //@PathVariable后端用了restfull，前端通过地址栏传值
    public R deleteUserById(@PathVariable Integer id) {
        boolean b = userSerivce.removeById(id);
        return R.out(b);
    }
    @PostMapping("/updateUser")
    //@RequestBody后端用了请求体，前端通过请求体传json
    public R updateUser(@RequestBody User user){
        boolean b = userSerivce.updateById(user);
        return R.out(b);
    }
    @GetMapping("/getUser/{id}")
    public R getUserById(@PathVariable Integer id){
        User user = userSerivce.getById(id);
        return R.putData("user", user);//这是成功的，R成功2个值，现在加上一个User总共三个值
    }
    @PostMapping("/getPage")
    public R getUserList(@RequestBody Page<User> page){
        Page<User> userPage = userSerivce.page(page);
        return R.putData("userpage",userPage);
    }

}